//
//  HNChoiceViewController.m
//  JHFBaseProject
//
//  Created by 朱传正 on 2017/1/4.
//  Copyright © 2017年 好牛. All rights reserved.
//

#import "HNChoiceViewController.h"
#import "HNChoiceCell.h"
#import "HNNormalMsgController.h"

@interface HNChoiceViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)NSMutableArray *dataSource;

@property (nonatomic,strong)HNNormalMsgController *normaoMsgVC;

@end

@implementation HNChoiceViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}


- (IBAction)backAction:(UIButton *)sender {
      [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)drawView{
    _dataSource = [[NSMutableArray alloc]init];
    
    [_tableView setBackgroundColor:RGB(236, 240, 241)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.bounces = NO;
    
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:_tableView];
}

-(void)initData{
    [self getChoiceListData];
}

#pragma -mark TableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"HNChoiceCell";
    
    HNChoiceCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil]firstObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([_choiceValue isEqualToString:@"hospital"]) {

    }else if ([_choiceValue isEqualToString:@"department"]){
     
        [_choiceCityBtn setHidden:YES];
        [_searchTextField setHidden:YES];
    }else if([_choiceValue isEqualToString:@"professionalTitle"]){

        [_choiceCityBtn setHidden:YES];
        [_searchTextField setHidden:YES];
    }
    
    cell.cellName.text = [NSString stringWithFormat:@"%@",[_dataSource[indexPath.row] objectForKey:@"name"]];
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [NSDictionary dictionaryWithDictionary:_dataSource[indexPath.row]];
    
    if ([_choiceValue isEqualToString:@"hospital"]) {
        [self.delegate HNChoiceViewControllerSetHospitai:dic];
        [self.navigationController popViewControllerAnimated:YES];
    }else if ([_choiceValue isEqualToString:@"department"]){
        [self.delegate HNChoiceViewControllerSetDepartment:dic];
        [self.navigationController popViewControllerAnimated:YES];
    }else if([_choiceValue isEqualToString:@"professionalTitle"]){
        [self.delegate HNChoiceViewControllerSetProfessionalTitle:dic];
        [self.navigationController popViewControllerAnimated:YES];

    }
    
}

#pragma mark - 初始化列表消息
-(void)getChoiceListData{

    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
    [param setObject:_choiceValue forKey:@"flag1"];
    NSLog(@"param = %@",param);
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_choice_value parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取医生列表失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            NSArray * choiceListArr = [data objectForKey:@"obj"];
            NSLog(@"------obj = %@",choiceListArr);
            for (int i = 0; i < choiceListArr.count; i++) {
                [_dataSource addObject:choiceListArr[i]];
            }
            
            NSLog(@"fdfhfgh%@",_dataSource);
            
            [self.tableView reloadData];
            
            
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                HNLoginViewController *loginVC = [[HNLoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
            [self.tableView.mj_footer endRefreshing];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取医生列表失败"];
        [self.tableView.mj_footer endRefreshing];
    }];

    
}







@end
